// 布
let cloth1 = [[0.73, 0.65], [0.4, 0.62], [0.3, 0.53], [0.24, 0.41], [0.18, 0.32], [0.42, 0.38], [0.35, 0.24], [0.31, 0.16], [0.28, 0.09], [0.55, 0.36], [0.51, 0.22], [0.5, 0.12], [0.49, 0.06], [0.66, 0.38], [0.64, 0.23], [0.64, 0.15], [0.64, 0.08], [0.74, 0.41], [0.75, 0.29], [0.76, 0.22], [0.75, 0.16]]
let cloth2 = [[0.33, 0.65], [0.64, 0.59], [0.73, 0.52], [0.79, 0.45], [0.88, 0.34], [0.57, 0.39], [0.66, 0.24], [0.67, 0.14], [0.66, 0.08], [0.41, 0.39], [0.37, 0.26], [0.32, 0.18], [0.34, 0.07], [0.29, 0.4], [0.23, 0.29], [0.17, 0.19], [0.15, 0.12], [0.23, 0.43], [0.16, 0.33], [0.11, 0.26], [0.08, 0.18]]

// 剪刀
let scissors1 = [[0.54, 0.72], [0.69, 0.6], [0.71, 0.46], [0.66, 0.35], [0.56, 0.3], [0.59, 0.38], [0.6, 0.25], [0.63, 0.16], [0.64, 0.06], [0.48, 0.41], [0.49, 0.26], [0.59, 0.35], [0.6, 0.44], [0.41, 0.45], [0.39, 0.32], [0.51, 0.39], [0.54, 0.48], [0.35, 0.5], [0.33, 0.38], [0.42, 0.41], [0.48, 0.48]]
let scissors2 = [[0.59, 0.67], [0.37, 0.57], [0.34, 0.47], [0.43, 0.4], [0.54, 0.38], [0.37, 0.42], [0.33, 0.27], [0.29, 0.19], [0.27, 0.11], [0.5, 0.41], [0.51, 0.24], [0.53, 0.15], [0.54, 0.06], [0.6, 0.42], [0.6, 0.31], [0.56, 0.38], [0.55, 0.44], [0.7, 0.45], [0.68, 0.36], [0.64, 0.42], [0.63, 0.46]]

// 拳头
let rock1 = [[0.51, 0.31], [0.22, 0.36], [0.07, 0.32], [0.06, 0.22], [0.13, 0.17], [0.09, 0.11], [0.04, 0.11], [0.08, 0.16], [0.13, 0.18], [0.23, 0.06], [0.16, 0.08], [0.19, 0.13], [0.23, 0.16], [0.35, 0.05], [0.28, 0.07], [0.29, 0.12], [0.32, 0.15], [0.45, 0.07], [0.38, 0.07], [0.39, 0.12], [0.41, 0.14]]
let rock2 = [[0.33, 0.36], [0.39, 0.29], [0.48, 0.17], [0.45, 0.1], [0.35, 0.12], [0.36, 0.12], [0.3, 0.03], [0.33, 0.08], [0.3, 0.18], [0.22, 0.15], [0.21, 0.05], [0.25, 0.15], [0.25, 0.23], [0.15, 0.18], [0.13, 0.08], [0.19, 0.16], [0.2, 0.23], [0.07, 0.21], [0.04, 0.13], [0.1, 0.16], [0.14, 0.22]]
let rock3 = [[0.31, 0.39], [0.37, 0.34], [0.44, 0.28], [0.5, 0.24], [0.48, 0.26], [0.29, 0.05], [0.34, 0.16], [0.35, 0.22], [0.33, 0.26], [0.18, 0.03], [0.26, 0.2], [0.29, 0.25], [0.29, 0.27], [0.11, 0.06], [0.16, 0.23], [0.21, 0.27], [0.24, 0.28], [0.07, 0.12], [0.09, 0.25], [0.12, 0.28], [0.15, 0.29]]
let rock4 = [[0.48, 0.34], [0.38, 0.34], [0.25, 0.29], [0.11, 0.22], [0.04, 0.16], [0.41, 0.07], [0.17, 0.06], [0.18, 0.12], [0.26, 0.17], [0.38, 0.07], [0.15, 0.08], [0.18, 0.15], [0.25, 0.17], [0.34, 0.11], [0.16, 0.11], [0.18, 0.16], [0.24, 0.19], [0.3, 0.14], [0.19, 0.15], [0.2, 0.17], [0.24, 0.19]]

let train_dataset = []

function initGestureData() {
    train_dataset.push(
        { label: '布', data: cloth1 },
        { label: '布', data: cloth2 },
        { label: '剪刀', data: scissors1 },
        { label: '剪刀', data: scissors2 },
        { label: '拳头', data: rock1 },
        { label: '拳头', data: rock2 },
        { label: '拳头', data: rock3 },
        { label: '拳头', data: rock4 },
    )
}

function getGesture(test) {
    // 需要有21个关键点
    if (test.length != 21) {
        return
    }

    // 计算目标手势和预定义手势之间的欧式距离
    const distance_list = []
    for (let i = 0; i < train_dataset.length; i++) {
        // 预定义手势
        const train = train_dataset[i]
        let distance = 0
        for (let j = 0; j < 21; j++) {
            const testItem = test[j]
            const trainItem = train.data[j]

            const x1 = testItem[0]
            const y1 = testItem[1]
            const x2 = trainItem[0]
            const y2 = trainItem[1]
            // 欧式距离
            const each_distance = Math.pow(x1 - x2, 2) +
                Math.pow(y1 - y2, 2)
            // 关键点的欧式距离之和
            distance += each_distance
        }
        // 保存计算结果
        distance_list.push({
            label: train.label,
            distance: distance
        })

    }

    // 按距离从小到大排序
    distance_list.sort(function (a, b) {
        return a.distance - b.distance
    })

    console.log(distance_list)

    // 当欧式距离太远时，则认为没有预定义的手势。
    if (distance_list[0].distance > 2.1) {
        console.log('distance', distance_list[0].distance)
        return '未知手势'
    }

    return distance_list[0].label
}

function dispose() {
    train_dataset = []
}

module.exports = {
    initGestureData,
    getGesture,
    dispose
}

